//
//  JJotherBottomView.m
//  JJHW_coreData
//
//  Created by lizhenqiang on 16/4/19.
//  Copyright © 2016年 HFKJ_LZQ. All rights reserved.
//

#import "JJotherBottomView.h"

@interface JJotherBottomView()
{
    
}

@end

@implementation JJotherBottomView

//@synthesize selectedRow = _selectedRow;
-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        _myWidth = frame.size.width;
        _myHeight = frame.size.height;
//        _tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, _myWidth, _myHeight)];
//        _tableview.delegate = self;
//        _tableview.dataSource = self;
        self.backgroundColor = NavBarBJColor;
    }
    
    return self;
}
-(void)setViewData:(NSMutableArray *)array andTitle:(NSString *)title
{
    /*
     
     KbottomViewTypeShoping=0,//鲸购物
     KbottomViewTypeMS , //鲸美食
     KbottomViewTypeGG,//鲸观光
     KbottomViewTypeDC,//鲸搭乘
     KbottomViewTypeKB//鲸快报
     */
    
    _objArray = array;
    _isScrollFlag = NO;
    if ([title isEqualToString:@"鲸购物"]) {
        self.bottomType = KbottomViewTypeShoping;
    }
    else if ([title isEqualToString:@"鲸美食"])
    {
        self.bottomType = KbottomViewTypeMS;
    }
    else if ([title isEqualToString:@"鲸观光"])
    {
        self.bottomType = KbottomViewTypeGG;
        _isScrollFlag = YES;
    }
    else if ([title isEqualToString:@"鲸搭乘"])
    {
        self.bottomType = KbottomViewTypeDC;
        
    }
    else if ([title isEqualToString:@"鲸快报"])
    {
        self.bottomType = KbottomViewTypeKB;
        _isScrollFlag = YES;
    }
    
     CGFloat btnHeight = _myHeight;
//    _objArray = array;
//    CGFloat width=0;
//    for (SplashChild *obj in array) {
//        width += obj.nameWidth;
//    }
//    if (width>DeviceWidth) {
//        _isScrollFlag = YES;
//    }
//    else
//    {
//        _isScrollFlag = NO;
//    }

    
    _tableview = [[UITableView alloc] initWithFrame:CGRectMake((DeviceWidth-_myHeight)/2, (_myHeight-DeviceWidth)/2, _myHeight, DeviceWidth)];
    _tableview.scrollEnabled = _isScrollFlag;
    _tableview.transform = CGAffineTransformMakeRotation(-M_PI/2);
    _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;//没有线
    _tableview.delegate = self;
    _tableview.dataSource = self;
    _tableview.showsHorizontalScrollIndicator = NO;
    _tableview.showsVerticalScrollIndicator = NO;
    _tableview.backgroundColor = NavBarBJColor;
    [_tableview registerNib:[UINib nibWithNibName:@"bottomViewCell" bundle:nil] forCellReuseIdentifier:@"bottomViewCell"];
//    [self addSubview:_allBtn];
    [self addSubview:_tableview];
    [_tableview reloadData];
    _selectedRow = 0;
    NSIndexPath *path = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableview selectRowAtIndexPath:path animated:YES scrollPosition:UITableViewScrollPositionNone];
    
}
-(void)loadAllDataRequest:(UIButton *)sender
{
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _objArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SplashChild *obj = _objArray[indexPath.row];
    if (_isScrollFlag) { //数据比较多
        if (indexPath.row == 0) {
            return kScaleFrom_iphone6_Desgin(75);
        }
        return kScaleFrom_iphone6_Desgin(84);
    }
    else
    {//不能滑动 数据比较少
        if (_objArray.count>0) {
            
            return DeviceWidth/(_objArray.count);
        }
    }
    
    return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    bottomViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"bottomViewCell"];
    cell.transform = CGAffineTransformMakeRotation(M_PI/2);
    SplashChild *obj = _objArray[indexPath.row];
    cell.nameLabel.text = obj.name;
    cell.backgroundColor = TabBarBjNormalColor;
    cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
    cell.selectedBackgroundView.backgroundColor = TabBarBjSelectColor;
    
    
    if (_isScrollFlag) { //数据比较多
        if (indexPath.row == 0) {
            CGFloat tmpWidth = (kScaleFrom_iphone6_Desgin(75)-obj.nameWidth+24)/2.0;
            cell.nameLabel.frame = CGRectMake(tmpWidth, 0, obj.nameWidth-24, _myHeight);
        }
        else
        {
            if (_objArray.count>0) {
                
                CGFloat width = (kScaleFrom_iphone6_Desgin(84)-obj.nameWidth+20)/2.0;
                cell.nameLabel.frame = CGRectMake(width, 0, obj.nameWidth-20, _myHeight);
            }
        }
        
    }
    else
    {//不能滑动 数据比较少
        if (_objArray.count>0) {
            
            CGFloat width = (DeviceWidth/_objArray.count-obj.nameWidth+20)/2;
            cell.nameLabel.frame = CGRectMake(width, 0, obj.nameWidth-20, _myHeight);
        }
    }
    
    
//    if (indexPath.row == 0) {
//
//        cell.nameLabel.frame = CGRectMake(12, 0, obj.nameWidth-24, _myHeight);
//    }
//    else
//    {
//        if (_isScrollFlag) { //数据比较多
//            
//            cell.nameLabel.frame = CGRectMake(10, 0, obj.nameWidth-20, _myHeight);
//        }
//        else
//        {//不能滑动 数据比较少
//            if (_objArray.count>0) {
//                
//                CGFloat width = (DeviceWidth-60)/(_objArray.count-1);
//                if (width>obj.nameWidth) {
//                    
//                   cell.nameLabel.frame = CGRectMake((width-(obj.nameWidth-20))/2, 0, obj.nameWidth-20, _myHeight);
//                }
//                else
//                {
//                    cell.nameLabel.frame = CGRectMake(5, 0, width-10, _myHeight);
//                }
//            }
//        }
//    }
    
    return cell;
    
    return nil;
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
//    NSArray *array = [tableView visibleCells];
//    for (bottomViewCell *cell in array) {
//        cell.backgroundColor = TabBarBjNormalColor;
//    }
//    bottomViewCell *cell
//    =(bottomViewCell*)[tableView cellForRowAtIndexPath:indexPath];
//    cell.backgroundColor = TabBarBjSelectColor;
    //去做相应的请求处理
    
    if (indexPath.row!=_selectedRow) {
        _selectedRow = (int)indexPath.row;
        SplashChild *obj = _objArray[_selectedRow];
        if (self.block) {
            self.block(obj);
        }
    }
    
    
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
